package com.northwest_man.gradle;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 列出重复的依赖包
 */
public class ListDuplicateLibrary {
    /**
     * libraries 文件夹的绝对路径
     */
    private final static String folderAbsPath = "/Users/h/Projects/app-rn/android/.idea/libraries";

    public static void main(String[] args) {
        final Collection<File> fileList = FileUtils.listFiles(new File(folderAbsPath), new String[]{"xml"}, false);
        final Map<String, ModuleVersion> libraryMap = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        for (final File file : fileList) {
            try {
                final String fileContent = FileUtils.readFileToString(file);
                final Pattern pattern = Pattern.compile("<library([^>]+)>");
                final Matcher matcher = pattern.matcher(fileContent);
                if (matcher.find()) {
                    final String libraryTag = matcher.group(1);

                    final Pattern libraryTagPattern = Pattern.compile("name\\s*=\\s*\"Gradle:\\s*([^\"]+)\"");
                    final Matcher libraryTagMatcher = libraryTagPattern.matcher(libraryTag);
                    if (libraryTagMatcher.find()) {
                        final String library = libraryTagMatcher.group(1);
                        final Pattern libraryPattern = Pattern.compile("([^ :]+?:[^: ]+?):([0-9.]+)");
                        final Matcher libraryMatcher = libraryPattern.matcher(library);
                        if (libraryMatcher.find()) {
                            final String libName = libraryMatcher.group(1);
                            final String newLibVersion = libraryMatcher.group(2);
                            if (libraryMap.containsKey(libName)) {
                                final ModuleVersion oldLibVersion = libraryMap.get(libName);
                                oldLibVersion.increaseCount();
                                oldLibVersion.newVersionName(newLibVersion);

                            } else {
                                libraryMap.put(libName, new ModuleVersion(newLibVersion));
                            }
                        } else {

                            System.out.println(String.format("文件(%s)中的 library (%s)标签不匹配预定格式", file.getName(), library));
                        }
                    } else {
                        System.out.println(String.format("文件(%s)中找不到 library", file.getName()));
                    }
                } else {

                    System.out.println(String.format("文件(%s)中找不到 library", file.getName()));
                }
            } catch (IOException e) {
                System.out.println(String.format("文件(%s)读取失败", file.getName()));
                e.printStackTrace();
            }
        }
//        Collections.sort(libraryMap);
        for (final Map.Entry<String, ModuleVersion> entry : libraryMap.entrySet()) {

//           System.out.println(String.format( "%d----%s:%s", entry.getValue().getCount(), entry.getKey() ,entry.getValue().getVersion()));
            if (entry.getValue().getCount() > 1) {
                System.out.println(String.format("%s:%s", entry.getKey(), entry.getValue().getVersion()));
            }
        }
    }
}
